﻿using System.Collections.Generic;
using System.Linq;
using Resn.WhuPub.BLL;
using Resn.WhuPub.DAL;
using Resn.WhuPub.DML;
using System;

namespace Resn.WhuPub.BLL
{
        /// <summary>
        /// 实现差旅的属性和方法
        /// </summary>
        public class TravelFlow :Entity<TravelFlowModel>
        {
                #region 构造函数

                /// <summary>
                /// 创建差旅实例
                /// </summary>
                /// <param name="model">差旅模型</param>
                public TravelFlow(TravelFlowModel model)
                        :base(model)
                {
                }

                #endregion


                #region 属性

                #region 基本属性

                /// <summary>
                /// 获取或设置流程状态
                /// </summary>
                /// <value>
                /// 流程状态
                /// </value>
                public TravelFlowState State
                {
                        get
                        {
                                return (TravelFlowState)Enum.Parse(typeof(TravelFlowState), Model.State);
                        }
                        set
                        {
                                Model.State = value.ToString();
                        }
                }

                /// <summary>
                /// 获取或设置出差员工标识
                /// </summary>
                /// <value>
                /// 出差员工标识
                /// </value>
                public string TravelEmployeeId
                {
                        get
                        {
                                return Model.TravelEmployeeId;
                        }
                }

                /// <summary>
                /// 获取或设置审批员工标识
                /// </summary>
                /// <value>
                /// 审批员工标识
                /// </value>
                public string ApproveEmployeeId
                {
                        get
                        {
                                return Model.ApproveEmployeeId;
                        }
                        set 
                        {
                                Model.ApproveEmployeeId = value;
                                this.NotifyPropertyChanged(i=>i.ApproveEmployeeId);
                        }

                }

                /// <summary>
                /// 获取或设置计划内容
                /// </summary>
                /// <value>
                /// 计划内容
                /// </value>
                public string PlanContent
                {
                        get
                        {
                                return Model.Content;
                        }
                        set
                        {
                                Model.Content = value;
                        }
                }

                

                /// <summary>
                /// 获取或设置计划修改意见
                /// </summary>
                /// <value>
                /// 计划修改意见
                /// </value>
                public string PlanModify
                {
                        get
                        {
                                return Model.Modify;
                        }
                        set
                        {
                                Model.Modify = value;
                        }
                }

                /// <summary>
                /// 获取或设置计划借款金额
                /// </summary>
                /// <value>
                /// 计划借款金额
                /// </value>
                public Double LendAmount
                {
                        get
                        {
                                return Model.LendAmount;
                        }
                        set
                        {
                                Model.LendAmount = value;
                        }
                }

                /// <summary>
                /// 获取或设置开始时间
                /// </summary>
                /// <value>
                /// 开始时间
                /// </value>
                public DateTime StartTime
                {
                        get
                        {
                                return Model.StartTime;
                        }
                        set
                        {
                                Model.StartTime = value;
                        }
                }

                /// <summary>
                /// 获取或设置结束时间
                /// </summary>
                /// <value>
                /// 结束时间
                /// </value>
                public DateTime EndTime
                {
                        get
                        {
                                return Model.EndTime;
                        }
                        set
                        {
                                Model.EndTime = value;
                        }
                }

                /// <summary>
                /// 获取或设置备注
                /// </summary>
                /// <value>
                /// 备注
                /// </value>
                public string Memo
                {
                        get
                        {
                                return Model.Memo;
                        }
                        set
                        {
                                Model.Memo = value;
                        }
                }

                #endregion 基本属性

                public Employee TravelEmployee
                {
                        get
                        {
                                var model = DBStorage.Instance.Select<EmployeeModel>(TravelEmployeeId);
                                return new Employee(model);
                        }
                }

                public Employee ApproveEmployee
                {
                        get
                        {
                                var model = DBStorage.Instance.Select<EmployeeModel>(ApproveEmployeeId);
                                return new Employee(model);
                        }
                }

                public string Name
                {
                        get { return TravelEmployee.Name + " " + StartTime.ToString("D") + " ---- " + EndTime.ToString("D"); }
                }

                /// <summary>
                /// 获取差旅流程下属的拜访事件
                /// </summary>
                /// <value>
                /// 差旅流程下属的拜访事件
                /// </value>
                public List<Visit> Visits
                {
                        get
                        {
                                var models = DBStorage.Instance.Select<VisitModel>().Where(i => i.TravelFlowId == Id).ToList();

                                var result = new List<Visit>();

                                models.ForEach(i => result.Add(new Visit(i)));

                                return result;
                        }
                }

                /// <summary>
                /// 获取差旅流程下属的拜访联系人
                /// </summary>
                /// <value>
                /// 差旅流程下属的拜访联系人
                /// </value>
                public List<Contact> Contacts
                {
                        get
                        {
                                return Visits.Select(visit => visit.Contact).ToList();
                        }
                }

                

                #endregion 属性

                #region 方法

                /// <summary>
                /// 获取指定标识的对象
                /// </summary>
                /// <param name="id">标识</param>
                /// <returns></returns>
                public static TravelFlow Select(string id)
                {
                        return Select<TravelFlow>(id);
                }
                /// <summary>
                /// 创建拜访
                /// </summary>
                /// <param name="contactId">联系人标识</param>
                /// <param name="startTime">开始时间</param>
                /// <param name="endTime">结束时间</param>
                /// <param name="visitType">拜访方式</param>
                /// <param name="objective">目的</param>
                /// <param name="keyPoints">要点</param>
                /// <param name="eatAmount">宴请金额</param>
                /// <param name="giftAmount">礼品金额</param>
                /// <param name="memo">备注</param>
                /// <returns>
                /// 创建的拜访对象
                /// </returns>
                public Visit CreateVisit(string contactId, DateTime startTime, DateTime endTime, VisitType visitType,
                                                      string objective, string keyPoints, double eatAmount, double giftAmount, string memo)
                {
                        var model = new VisitModel
                                {
                                        Id = Guid.NewGuid().ToString(),
                                        TravelFlowId = Id,
                                        ContactId = contactId,
                                        StartTime = startTime,
                                        EndTime = endTime,
                                        VisitType = visitType.ToString(),
                                        Objective = objective,
                                        KeyEvents = keyPoints,
                                        EatAmount = eatAmount,
                                        GiftAmount = giftAmount,
                                        EnteringTime = DateTime.Now,
                                        Memo = memo
                                };
                        return new Visit(model);
                }

                public bool IsContainsContact(Contact contact)
                {
                        var contacts = Contacts;
                        return contacts.Any(i=>i.Id == contact.Id);
                }
                
                #endregion

        }
}